var User = $.Class.create({
	
	_loginUrl : '/login/auth/format/json',
	_logoutUrl : '/login/logout/format/json',
		
	// constructor		
	initialize : function(email) {
		this._email = email;
	},

	
	/* Handlers */
	
	addHomePageHandlers : function() {
		self = this;

		$('#btnLogin').click(function() {
			var options = {
				email: $('#email').val(),
				password: $('#password').val(),
				complete: self.completeLoginHomePage,
				beforeSend: self.beforeSendLoginHomePage,
				error: self.errorLoginHomePage
			};
			self.authenticate(options);
			return false;
		});
		
		$('#btnLogout').click(function() {
			var options = {
				complete: self.completeLogoutHomePage,
				beforeSend: self.beforeSendLogoutHomePage,
				error: self.errorLogoutHomePage
			};
			self.logout(options);
			return false;
		});		
				
	},
	
	
	/* Logout */
	
	logout : function(options) {
		if (!options.url) options.url = this._logoutUrl;		
		$.ajax({
			type: "POST",
			url: options.url,
			dataType: "json",
			beforeSend: options.beforeSend,			
			error: options.error,
		 	complete: options.complete
		});
	},	
	
	beforeSendLogoutHomePage : function(jqXHR, settings) {
		var liWidth = $('#divUserLogged').width();
		var liHeight = $('#divUserLogged').height();
		// To be sure we can set the background position
		$('#divUserLogged').css('display', 'block');
		// Hide the container with the text and button
		$('#divUserLogged div').fadeOut('fast');				
		// Setup the size of the bigger container to show the loader animation
		$('#divUserLogged').width(liWidth + 'px');		
		$('#divUserLogged').height(liHeight + 'px');
		// Show the loader animation
		$('#divUserLogged').css('background-position', '50% 50%');
		
	},
	
	completeLogoutHomePage : function(jqXHR, textStatus) {		
		$('#divUserLogged').css('background-position', '50% 1000px');
		var user = $.parseJSON(jqXHR.responseText);
		if (!user.email) {
			// Hide the loader animation container
			$('#divUserLogged').fadeOut('fast');
			// Show the login form conatiners			
			$('#liLoginLoader div').fadeIn('fast');						
			$('#liLogin div').fadeIn('fast');
		} else {
			// If it fails, show the text and logout button again
			$('#divUserLogged div').fadeIn('fast');
		}		
	},
	
	errorLogoutHomePage : function (jqXHR, textStatus, errorThrown) {
		alert("Ha ocurrido un error grave");
		$('#divUserLogged div').fadeIn('fast');
		$('#divUserLogged').css('background-position', '50% 1000px');
	},	
	
	/* Authentication */
	
	authenticate : function(options) {
		if (!options.url) options.url = this._loginUrl;		
		$.ajax({
			type: "POST",
			url: options.url,
			dataType: "json",
			data: 'email=' + options.email + '&password=' + options.password,
			beforeSend: options.beforeSend,			
			error: options.error,
		 	complete: options.complete
		});
	},
	
	
	errorLoginHomePage : function (jqXHR, textStatus, errorThrown) {
		alert("Ha ocurrido un error grave");
		$('#liLoginLoader div').fadeIn('fast');
		$('#liLoginLoader').css('background-position', '50% 1000px');
	},
	
	beforeSendLoginHomePage : function(jqXHR, settings) {
		$('#email').attr('disabled', true);
		$('#password').attr('disabled', true);
		var liWidth = $('#liLoginLoader').width();
		var liHeight = $('#liLoginLoader').height();		
		// Hide the left container with the button and link
		$('#liLoginLoader div').fadeOut('fast');			
		// To be sure that we can set the background position
		$('#liLoginLoader').css('display', 'block');
		// Setup the loader animation container size
		$('#liLoginLoader').width(liWidth + 'px');		
		$('#liLoginLoader').height(liHeight + 'px');
		// Show the loader animation
		$('#liLoginLoader').css('background-position', '50% 50%');		
	},
	
	completeLoginHomePage : function(jqXHR, textStatus) {		 		
		$('#email').attr('disabled', false);
		$('#password').attr('disabled', false);
		
		var user = $.parseJSON(jqXHR.responseText); 		
 		if (user) {
 			// Hide the loader animation
 			$('#liLoginLoader').css('background-position', '50% 1000px');
 			// Hide the entire form container
 			$('#liLogin div').fadeOut('fast');
 			// Show the user email and show the container
 			$('#divUserLogged span').html(user.email);
 			$('#divUserLogged div').show();
 			$('#divUserLogged').fadeIn('fast');
 		}
 		
 		var url = jqXHR.getResponseHeader('X-Redirect');
 		if (url) $(location).attr('href',url); 		
	}
	
}, {
	// properties
	getset : [
	          [ 'email', '_email' ],
	          [ 'loginUrl', '_loginUrl' ],
	          [ 'logoutUrl', '_logoutUrl' ]
	]
});
















/*

$(function() {
	// person class
	var Person = $.Class.create({
		// constructor
		initialize : function(name, family, father_name) {
			this._name = name;
			this._family = family;
			this._father_name = father_name;
		},
		// methods
		ClassName : function() {
			alert('Person Class');
		},
		toString : function() {
			return this.property('Name') + ' ' + this.property('Family')
					+ ' Son of ' + this.property('FatherName') + ' '
					+ this.property('Family');
		}
	}, {
		// properties
		getset : [ [ 'Name', '_name' ], [ 'Family', '_family' ],
				[ 'FatherName', '_father_name' ] ]
	});
	var john = new Person('John', 'Foster', 'Bill');
	alert(john);
	john.addProperty('Mother', '_mother');
	john.property('Mother', 'Tatiana');
	alert(john.property('Mother'));
	john.ClassName();
	// student class
	var Student = $.Class.create(Person, {
		// constructor
		initialize : function(name, family, father_name, student_code) {
			this.base('initialize', name, family, father_name, student_code);
			this._student_code = student_code;
		},
		// methods
		ClassName : function() {
			this.base('ClassName');
			alert('Student Class');
		},
		toString : function() {
			return this.property('Name') + ' ' + this.property('Family')
					+ ' Son of ' + this.property('FatherName') + ' '
					+ this.property('Family') + ' Student Code is:'
					+ this.property('StudentCode');
		}
	}, {
		// properties
		getset : [ [ 'StudentCode', '_student_code' ] ]
	});
	var johnStudent = new Student('John', 'Foster', 'Bill', 1001);
	alert(johnStudent);
	johnStudent.ClassName();
	// employee class
	var Employee = $.Class.create(Person, {
		// properties
		getEmployeeCode : function() {
			return this._employee_code;
		},
		setEmployeeCode : function() {
			this._employee_code = arguments[0];
		},
		// constructor
		initialize : function(name, family, father_name, employee_code) {
			this.base('initialize', name, family, father_name, employee_code);
			this._employee_code = employee_code;
		},
		// methods
		toString : function() {
			return this.property('Name') + ' ' + this.property('Family')
					+ ' Son of ' + this.property('FatherName') + ' '
					+ this.property('Family') + ' Employee Code is:'
					+ this.property('EmployeeCode');
		}
	});
	var johnEmployee = new Employee('John', 'Foster', 'Bill', 5001);
	alert(johnEmployee);

	// base car class
	var BaseCar = $.Class.create({
		// constructor
		initialize : function(name, model) {
			this._name = name;
			this._model = model;
		},
		// methods
		toString : function() {
			return this.property('Name') + ' ' + this.property('Model');
		}
	}, {
		// properties
		getset : [ [ 'Name', '_name' ], [ 'Model', '_model' ] ],
		// settings
		abstract : true
	});
	try {
		var toyota = new BaseCar('Toyota', '2008');
		alert(toyota);
	} catch (ex) {
		alert(ex);
	}

	// car class
	var Car = $.Class.create(BaseCar);
	var toyota = new Car('Toyota', '2008');
	alert(toyota);

	// computer interface
	var computerInterface = new $.Interface();
	computerInterface.attributes = [ '_name', '_prosessor' ];
	computerInterface.properties = [ 'Name', 'Prosessor' ];
	computerInterface.methods = [ 'turnOn', 'turnOff' ];

	try {
		var computerWidthError = $.Class.create({
								// constructor
					initialize : function(name, prosessor) {
						this._name = name;
						this._prosessor = prosessor;
					},
					// methods
					toString : function() {
						return this.property('Name') + ' '
								+ this.property('Prosessor');
					}
				}, {
					// properties
					getset : [ [ 'Name', '_name' ] ],
					// settings
					implements : [ computerInterface ]
				// can be more than one
				});
	} catch (ex) {
		alert(ex);
	}

	var computer = $.Class.create({
		// attributes
		_name : null,
		_prosessor : null,
		// constructor
		initialize : function(name, prosessor) {
			this._name = name;
			this._prosessor = prosessor;
		},
		// methods
		toString : function() {
			return this.property('Name') + ' ' + this.property('Prosessor');
		},
		turnOn : function() {
			alert("i'm on");
		},
		turnOff : function() {
			alert("i'm off");
		}
	}, {
		// properties
		getset : [ [ 'Name', '_name' ], [ 'Prosessor', '_prosessor' ] ],
		// settings
		implements : [ computerInterface ]
	// can be more than one
	});
	var pc = new computer('Pc', 'Intel');
	pc.turnOn();
	pc.turnOff();

	// person module
	var Mine = $.Class.create({
		// methods
		Hi : function() {
			alert('Hi, My name is hassan jodat shandi');
		}
	}, {
		// settings
		module : 'Hwt.Test.Hassan'
	});
	Hwt.Test.Hassan.Hi();
});

*/